﻿#region LGPL
/************************************************************************************/
/* Fireball.Windows.Forms a controls library for windows forms
 * 
 * Copyright (C) 2009 Sebastian Faltoni
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 59 
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Signature of Sebastian Faltoni, 2 Febbraio 2009
 * Sebastian Faltoni, President of Orangebit
 * 
 ***********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace Orangebit.Windows.Forms
{
    /// <summary>
    /// The ExplorerBar is a Control that mimic the navigation style used on Windows XP folders
    /// </summary>
    public class ExplorerBar : StackPanel
    {

        private static readonly object ThemeChangedEvent = new object();

        public event EventHandler ThemeChanged
        {
            add
            {
                this.Events.AddHandler(ThemeChangedEvent, value);
            }

            remove
            {
                this.Events.RemoveHandler(ThemeChangedEvent, value);
            }
        }

        public ExplorerBar()
        {
            this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer
                | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint
                | System.Windows.Forms.ControlStyles.ResizeRedraw
                | System.Windows.Forms.ControlStyles.ContainerControl
                | System.Windows.Forms.ControlStyles.SupportsTransparentBackColor
                | System.Windows.Forms.ControlStyles.UserPaint, true);
            this.theme = new DefaultExplorerBarTheme();
        }


        private IExplorerBarTheme theme = null;

        /// <summary>
        /// Get or Set the ExplorerBar Theme
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IExplorerBarTheme Theme
        {
            get
            {
                if (theme == null)
                    return new DefaultExplorerBarTheme();
                return theme;
            }
            set
            {
                if (value == null)
                    throw new Exception("The ExplorerBar Theme Property canno't be null");
                theme = value;
                this.OnThemeChanged(EventArgs.Empty);
                this.Invalidate(true);
            }
        }

        protected void OnThemeChanged(EventArgs e)
        {
            EventHandler handler = (EventHandler)base.Events[ThemeChangedEvent];
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            //base.OnPaint(e);
            this.theme.PaintExplorerBar(this, e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            foreach (Control c in this.Controls)
            {
                c.Refresh();
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.theme.OnExplorerBarSizeChanged(this);
            this.Invalidate(true);
        }
    }
}
